package leesoft.configuration.xmlconfig;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leesoft.configuration.databeans.DataSource;
import leesoft.configuration.databeans.Integration;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.Element;

public class Configuration {
	
	Map<String,DataSource> dsSet=new HashMap<String,DataSource>();
	List<Integration> integrationsList=new ArrayList<Integration>();
	
	@SuppressWarnings("unchecked")
	public Configuration(String xmlfilepath){
		SAXReader reader=new SAXReader();
		try{
			Document document=reader.read(new File(xmlfilepath));
			Element root=document.getRootElement();
			List<Element> datasourceNodeList=(List<Element>)root.elements("datasource");
			for(Element datasourceNode:datasourceNodeList){
				String datasourceName=datasourceNode.attributeValue("name");
				DataSource datasource=new DataSource();
				datasource.convertXml(datasourceNode);
				dsSet.put(datasourceName,datasource);
			}
			//now let's parse the integrations part
			
			List<Element> integrationsE=root.element("integrations").elements("integration");
			for(Element integrationE:integrationsE){
				Integration integration=new Integration();
				integration.convertXml(integrationE);
				integrationsList.add(integration);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public List<Integration> getIntegrations(){
		return integrationsList;
	}
	public DataSource getDS(String name){
		return dsSet.get(name);
	}
	
}
